import verb,noun,prep,framesyntax  # @UnresolvedImport

class prepsentence(object):
   def __init__(self, annotatedwordlist):
      self.sentence = annotatedwordlist

   @property
   def verb(self):
      for word in self.sentence:
         if type(word) == verb.verb:
            return word

   @property
   def prep(self):
      for word in self.sentence:
         if type(word) == prep.prep:
            return word

class withsentence(prepsentence):
   def __init__(self, annotatedwordlist):
      super(withsentence, self).__init__(annotatedwordlist)

   '''For the use of this preposition, we can get some valuable information
   by considering only what follows the preposition.'''
   @property
   def _instrumentalframes(self):
      matches = []
      '''Figure out where "with" is in this sentence so that we can compare
      with the frames.'''
      for sindex, word in enumerate( self.sentence ):
         if type(word) == prep.prep and word.value == 'with':
            break;
      '''Find "with" in the frames. Then compare the self.sentence with the
      frame.'''
      for frame in self.verb.frameswithprep('with'):
         match = True
         for findex, word in enumerate( frame ):
            if type(word) == framesyntax.syntaxPrep and 'with' in word.values:
               break
         '''words after 'with'''
         for offset in xrange(1,len(frame)-findex):
            if not frame[findex+offset].match( self.sentence[sindex+offset] ):
               match = False
               break
         if match: matches.append(frame) 
      return matches


   '''Ratnaparkhi's data set is composed of (verb, noun, prep, noun) tuples.
   To match this sort of data, do not consider anything to the left of the
   verb in order to match sentence structure.'''
   @property
   def _ratnaparkhiframes(self):
      matches = []
      '''Figure out where "with" is in this sentence so that we can compare
      with the frames.'''
      for sindex, word in enumerate( self.sentence ):
         if type(word) == prep.prep and word.value == 'with':
            break;
      '''Find "with" in the frames. Then compare the self.sentence with the
      frame.'''
      for frame in self.verb.frameswithprep('with'):
         match = True
         for findex, word in enumerate( frame ):
            if type(word) == framesyntax.syntaxPrep and 'with' in word.values:
               break
         for offset in xrange(1,len(frame)-findex):
            if not frame[findex+offset].match( self.sentence[sindex+offset] ):
               match = False
               break
         for offset in xrange(1,findex-frame.verbindex):
            if not frame[findex-offset].match( self.sentence[sindex-offset] ):
               #print frame[findex-offset], self.sentence[sindex-offset]
               match = False
               break
         if match: matches.append(frame) 
      return matches

   '''a perfectly matched frame is a verbnet frame syntax which precisely
   matches the structure of this sentence.'''
   @property
   def _perfectmatchframes(self):
      matches = []
      '''Figure out where "with" is in this sentence so that we can compare
      with the frames.'''
      for sindex, word in enumerate( self.sentence ):
         if type(word) == prep.prep and word.value == 'with':
            break;
      '''Find "with" in the frames. Then compare the self.sentence with the
      frame.'''
      for frame in self.verb.frameswithprep('with'):
         match = True
         for findex, word in enumerate( frame ):
            if type(word) == framesyntax.syntaxPrep and 'with' in word.values:
               break
         '''words after 'with'''
         for offset in xrange(1,len(frame)-findex):
            if not frame[findex+offset].match( self.sentence[sindex+offset] ):
               match = False
               break
         '''words before with'''
         for offset in xrange(1,findex+1):
            if not frame[findex-offset].match( self.sentence[sindex-offset] ):
               match = False
               break
         if match: matches.append(frame) 
      return matches
